//
//  XRBaseViewController.m
//  LiveIMTool
//
//  Created by NeiQuan on 16/10/26.
//  Copyright © 2016年 AlexiChen. All rights reserved.
//

#import "XRBaseViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
@interface XRBaseViewController ()

@end

@implementation XRBaseViewController

- (void)dealloc
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)addOwnViews
{
    [super addOwnViews];
    
}
-(void)configOwnViews
{
    [super configOwnViews];
    
}
- (void)addTapBlankToHideKeyboardGesture;
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapBlankToHideKeyboard:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tap];
}

- (void)onTapBlankToHideKeyboard:(UITapGestureRecognizer *)ges
{
    if (ges.state == UIGestureRecognizerStateEnded)
    {
        [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    }
}
- (void)callImagePickerActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
    actionSheet.cancelButtonIndex = 2;
    [actionSheet showInView:self.view];
}
//在这里需要提醒用户去开启相册和相机
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        return;
    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    
    if (buttonIndex==0)
    {
        AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
        {//无权限
            
//            [XRCommonPopTipView popMenuWithtitletext:@"温馨提示" contentText:@"你已经拒绝访问X课堂访问你的相机！赶紧去开启吧" toPopView:self.view type:^(SendKeyBtnType type) {
//                
//                if (type==SendKeyBtnTypeDone)
//                {
//                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"Photos://"]];
//                }
//            }];
            return;
        }else
        {
          imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
          [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }else if (buttonIndex==1)
    {
        if ([self isCanUsePhotos])
        {
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else
        {//无权限
//            [XRCommonPopTipView popMenuWithtitletext:@"温馨提示" contentText:@"你已经拒绝访问X课堂访问你的相册！赶紧去开启吧" toPopView:self.view type:^(SendKeyBtnType type) {
//                
//                if (type==SendKeyBtnTypeDone)
//                {
//                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"Photos://"]];
//                }
//            }];
            return;
        }
    }
}
- (BOOL)isCanUsePhotos {
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) {
            //无权限
        return NO;
        }
    }
    else {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted ||
            status == PHAuthorizationStatusDenied) {
            //无权限
            return NO;
        }
    }
    return YES;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
   
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
//#pragma mark 处理点击空白处 键盘收回事件
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    if (![self isMemberOfClass:[UIScrollView class]])
//    {
//        
//    } else {
//        [[self nextResponder] touchesBegan:touches withEvent:event];
//        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
//            [super touchesBegan:touches withEvent:event];
//        }
//    }
//    
//}
//-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//    if (![self isMemberOfClass:[UIScrollView class]]) {
//        
//    } else {
//        [[self nextResponder] touchesMoved:touches withEvent:event];
//        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
//            [super touchesMoved:touches withEvent:event];
//        }
//    }
//}
//- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//    if (![self isMemberOfClass:[UIScrollView class]]) {
//        
//    } else {
//        [[self nextResponder] touchesEnded:touches withEvent:event];
//        if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
//            [super touchesEnded:touches withEvent:event];
//        }
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
